package com.chb.app.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.chb.app.mapper.CareOrderItemMapper;
import com.chb.app.mapper.OrderChargeItemMapper;
import com.chb.app.mapper.OrderChargeMapper;
import com.chb.app.mapper.OrderItemMapper;
import com.chb.app.service.PayService;
import com.hos.entity.his.CareOrderItem;
import com.hos.entity.his.OrderCharge;
import com.hos.entity.his.OrderChargeItem;
import com.hos.vo.Result;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author: 陈海彬
 * @date: Created in 2024/1/6 18:26
 * @description: 订单监听器
 * @modified By:
 * @version:
 */
@Component
@Slf4j
public class MyListener {


    @Resource
    private OrderChargeMapper orderChargeMapper;
    @Resource
    private OrderChargeItemMapper orderChargeItemMapper;
    @Resource
    private CareOrderItemMapper careOrderItemMapper;
    @Resource
    private PayService payService;

    @RabbitListener(queues = "order_queue")
    public void fund(Message message, Channel channel) {
        try {
            Result<String> result = payService.queryWxPayStatus(new String(message.getBody()));
            if (result.getCode() == 200) {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
            } else {
                Thread.sleep(1000);
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param message 队列信息
     * @create by: 陈海彬
     * @description: 死信队列监听信息
     * @create time: 2024/1/6 20:13
     */
    @RabbitListener(queues = {"dlx_queue"})
    public void dlxQueueMessage(Message message, Channel channel) {
        // byte[] body = message.getBody();
        String chId = new String(message.getBody());
        OrderCharge orderCharge = this.selectOrderChargeByStatus(chId);
        // log.info("消费者接收到延迟订单【{}】", orderCharge.getOrderId());
        if (orderCharge != null) {
            // 查询订单的支付状态
            if ("0".equals(orderCharge.getOrderStatus())) {
                // 将订单状态设置成已超时
                orderCharge.setOrderStatus("2");
                this.orderChargeMapper.updateById(orderCharge);
                // 设置原来的为已取消状态
                this.updateOrderItem(orderCharge.getOrderId());
            }
            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }


    }

    /**
     * @param orderId
     * @return void
     * @create by: 陈海彬
     * @description: 修订病例的状态为已取消
     * @create time: 2024/1/9 16:39
     */
    private void updateOrderItem(String orderId) {
        LambdaQueryWrapper<OrderChargeItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderChargeItem::getOrderId, orderId);
        List<OrderChargeItem> chargeItemList = this.orderChargeItemMapper.selectList(wrapper);
        for (OrderChargeItem orderChargeItem : chargeItemList) {
            LambdaUpdateWrapper<CareOrderItem> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(CareOrderItem::getItemId, orderChargeItem.getItemId())
                    .set(CareOrderItem::getStatus, "5");
            this.careOrderItemMapper.update(null, updateWrapper);
        }
    }

    /**
     * @param chId 病例id
     * @return void
     * @create by: 陈海彬
     * @description: 修改订单的支付状态
     * @create time: 2024/1/6 20:38
     */
    private void updateOrderChargeStatus(String chId) {
        LambdaUpdateWrapper<OrderCharge> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(OrderCharge::getOrderId, chId)
                .set(OrderCharge::getOrderStatus, "2");
        this.orderChargeMapper.update(null, wrapper);
    }


    /**
     * @param chId 患者id
     * @return com.hos.entity.his.OrderCharge
     * @create by: 陈海彬
     * @description: 查询订单的支付状态
     * @create time: 2024/1/6 20:33
     */
    private OrderCharge selectOrderChargeByStatus(String chId) {
        LambdaQueryWrapper<OrderCharge> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderCharge::getOrderId, chId);
        return this.orderChargeMapper.selectOne(wrapper);
    }
}